<template>
  <div class='xtx-goods-page' >
    <div class="container" v-if="goodsData.categories">
     <GoodsBread :goodsData="goodsData"/>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goodsData.mainPictures" />
          <GoodsSales />
        </div>
        <div class="spec">
            <GoodsName :goodsData="goodsData"/>
            <!-- 规格组件 -->
            <GoodsSku :goodsData="goodsData"  @change="skuChange" skuId="300042048"/>
            <!-- 数量选择组价、 -->
            <XtxNumbox  label="数量" v-model="num" :min="1" :max="goodsData.inventory" />
              <!-- 按钮 -->
              <XtxButton style="margin-top:20px" type='primary' @click="insertCart" size='middle'>加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId='goodsData.id'/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs/>
          <!-- 注意事项 -->
          <GoodsWarn/>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" :goodId="goodsData.id"></GoodsHot>

          <GoodsHot :type="2" :goodId="goodsData.id"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useStore } from 'vuex'
import GoodsWarn from './cpmponents/goods-warn.vue'
import GoodsHot from './cpmponents/goods-hot.vue'
import GoodsTabs from './cpmponents/goods-tabs.vue'
import GoodsRelevant from './cpmponents/goods-relevant.vue'
import GoodsSku from './cpmponents/goods-sku.vue'
import GoodsSales from './cpmponents/goods-sales.vue'
import GoodsImage from './cpmponents/goods-image.vue'
import GoodsBread from './cpmponents/goods-bread.vue'
import GoodsName from './cpmponents/goods-name.vue'
import { useRoute } from 'vue-router'
import { findGoods } from '@/api/goods'
import { ref, provide } from 'vue'
import Message from '@/components/message'

export default {
  name: 'XtxGoodsPage',
  components: { GoodsBread, GoodsImage, GoodsSales, GoodsName, GoodsSku, GoodsRelevant, GoodsTabs, GoodsHot, GoodsWarn },
  setup () {
    const route = useRoute()
    const goodsData = ref({})
    findGoods(route.params.id).then(data => {
      console.log('findGoods', data)
      goodsData.value = data.result
    })
    // 把数据传给后代
    provide('goodsData', goodsData)
    const currSku = ref(null)
    const skuChange = (sku) => {
      if (sku.skuId) {
        goodsData.value.price = sku.price
        goodsData.value.oldPrice = sku.oldPrice
        goodsData.value.inventory = sku.inventory
        console.log(sku.inventory, '222222222222222')
        currSku.value = sku
      } else {
        currSku.value = null
      }
    }
    const num = ref(1)
    const store = useStore()
    const insertCart = () => {
      if (!currSku.value) {
        Message({ type: 'error', text: '请选择规格' })
        return
      }
      if (num.value > currSku.value.inventory) {
        Message({ type: 'error', text: '数量超出了库存' })
        return
      }
      // 1.收集参数（有效性判断）
      // 2.调用action
      const cart = {
        id: goodsData.value.id, // 商品id
        skuId: currSku.value.skuId, // 有效skuId
        name: goodsData.value.name, // 商品名称
        picture: goodsData.value.mainPictures[0], // 商品图片 - 非必须
        price: currSku.value.oldPrice, // 原价格
        nowPrice: currSku.value.price, // 现价格
        count: num.value, // 商品数量
        attrsText: currSku.value.specsText, // 规格文案
        selected: true, // 是否选中
        stock: currSku.value.inventory, // 库存数量
        isEffective: true // 是否有效商品
      }
      console.log('要增加的商品是', cart)
      // 2.调用action做添加
      store.dispatch('cart/insertCart', cart).then(() => {
        Message({ type: 'success', text: '添加成功' })
      })
    }
    return { goodsData, skuChange, num, insertCart }
  }

}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
